/*
 *  $Id: BlockLayoutEditor.java 288 2008-10-26 13:30:28Z martinlaz $
 *
 *  Author:
 *     Martin Lazarov [mlazarov at sfs.uni-tuebingen.de]
 *     
 *  This file is part of the Gralej system
 *     http://code.google.com/p/gralej/
 *
 *  Gralej is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Gralej is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package gralej.blocks.configurator;

import gralej.blocks.BlockLayout;
import gralej.blocks.BlockPanelStyle;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 *
 * @author  Martin
 */
public class BlockLayoutEditor extends javax.swing.JDialog {
    private BlockPanelStyle _panelStyle;
    private BlockLayout _layout;
    private boolean _isInitializing;
    
    /** Creates new form BlockLayoutEditor */
    public BlockLayoutEditor(
            java.awt.Window parent,
            boolean modal,
            BlockPanelStyle style) {
        super(parent);
        setModal(modal);
        initComponents();
        setLocationRelativeTo(parent);
        
        JComponent c = (JComponent) getContentPane();
        c.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                KeyStroke.getKeyStroke("ESCAPE"), "cancel");
        c.getActionMap().put("cancel",
            new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
        
        setBlockPanelStyle(style);
    }
    
    public void setBlockPanelStyle(BlockPanelStyle blockStyle) {
        _panelStyle = blockStyle;
    }
    
    public void reset(BlockLayout layout) {
        _isInitializing = true;
        _layout = layout;
        _layoutName.setText(layout.getName());
        _spLeading.setValue(_layout.getLeadingSpace());
        _spIntra.setValue(_layout.getIntraSpace());
        _spTrailing.setValue(_layout.getTrailingSpace());
        _isInitializing = false;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        _spLeading = new javax.swing.JSpinner();
        _spIntra = new javax.swing.JSpinner();
        _spTrailing = new javax.swing.JSpinner();
        jPanel2 = new javax.swing.JPanel();
        _layoutName = new javax.swing.JLabel();

        setTitle("BlockLayout Editor");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Spaces"));

        jLabel1.setText("Leading:");

        jLabel2.setText("Intra:");

        jLabel3.setText("Trailing:");

        _spLeading.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
        _spLeading.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _spLeadingStateChanged(evt);
            }
        });

        _spIntra.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
        _spIntra.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _spIntraStateChanged(evt);
            }
        });

        _spTrailing.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
        _spTrailing.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _spTrailingStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(_spLeading, javax.swing.GroupLayout.PREFERRED_SIZE, 40, Short.MAX_VALUE)
                    .addComponent(_spIntra)
                    .addComponent(_spTrailing))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(_spLeading, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(_spIntra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(_spTrailing, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Layout Name"));

        _layoutName.setFont(new java.awt.Font("Tahoma", 1, 11));
        _layoutName.setText("Layout name");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(_layoutName)
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(_layoutName)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _spLeadingStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__spLeadingStateChanged
        if (_isInitializing) return;
        _layout.setLeading((Integer) _spLeading.getValue());
        _panelStyle.fireStyleChanged();
    }//GEN-LAST:event__spLeadingStateChanged

    private void _spIntraStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__spIntraStateChanged
        if (_isInitializing) return;
        _layout.setIntra((Integer) _spIntra.getValue());
        _panelStyle.fireStyleChanged();
    }//GEN-LAST:event__spIntraStateChanged

    private void _spTrailingStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__spTrailingStateChanged
        if (_isInitializing) return;
        _layout.setTrailing((Integer) _spTrailing.getValue());
        _panelStyle.fireStyleChanged();
    }//GEN-LAST:event__spTrailingStateChanged
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel _layoutName;
    private javax.swing.JSpinner _spIntra;
    private javax.swing.JSpinner _spLeading;
    private javax.swing.JSpinner _spTrailing;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
    
}
